package com.diudiutechnology.www.yadiudiu.yadiudiu.main;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.widget.FrameLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.diudiutechnology.www.yadiudiu.util.utils.UserHelper;
import com.diudiutechnology.www.yadiudiu.yadiudiu.car.BuyACarFragment;
import com.diudiutechnology.www.yadiudiu.yadiudiu.house.HousePurchaseFragment;
import com.diudiutechnology.www.yadiudiu.yadiudiu.mine.MineFragment;
import com.diudiutechnology.www.yadiudiu.R;
import com.diudiutechnology.www.yadiudiu.yadiudiu.singlecall.CalledSingleFragment;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2017/8/14.
 * 主题板块
 */

public class HomePageFragmentActivity extends FragmentActivity  {
    @Bind(R.id.home_page_enter)
    FrameLayout homePageEnter;
    @Bind(R.id.nar_menu)
    RadioGroup narMenu;
    private List<Fragment> fragmentList;
    private int lastPosition;
    HomePageFragment homePageFragment;
    BuyACarFragment buyACarFragment;
    HousePurchaseFragment housePurchaseFragment;
    CalledSingleFragment calledSingleFragment;
    MineFragment mineFragment;
    public  static  HomePageFragmentActivity activity;
    // 定位相关
    LocationClient mLocClient;
    boolean isFirstLoc = true; // 是否首次定位
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home_page_fragment);
        ButterKnife.bind(this);
        activity=this;
        Location();
        initView();

    }

/**
 * 初始化控件
 */
    private void initView() {
        narMenu = (RadioGroup) findViewById(R.id.nar_menu);
        initFragment();
        lastPosition = 0;
        loadFragment(0);
        ((RadioButton) narMenu.getChildAt(0)).setChecked(true);

        narMenu.setOnCheckedChangeListener(checkedChangeListener);
    }
    /**
     * RadioGroup点击事件
     */
    private RadioGroup.OnCheckedChangeListener checkedChangeListener = new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            switch (checkedId) {
                case R.id.nar_home_page:
                    lastPosition = 0;
                    loadFragment(0);

                    break;
                case R.id.buy_a_car:
                    lastPosition = 1;
                    loadFragment(1);
                    buyACarFragment.processscrollTo();
                    break;
                case R.id.house_purchase:
                        lastPosition = 2;
                        loadFragment(2);
                    housePurchaseFragment.processscrollTo();
                    break;
                case R.id.called_single:
                    lastPosition = 3;
                    loadFragment(3);
                    break;
                case R.id.mine:
                    if (UserHelper.isLoginWithAction(HomePageFragmentActivity.this)) {
                        lastPosition = 4;
                        loadFragment(4);
                        mineFragment.processout();
                    } else {
                        ((RadioButton) narMenu.getChildAt(lastPosition)).setChecked(true);
                    }

                    break;

            }
        }
    };
    /**
     * 初始化Fragment
     */
    private void initFragment() {
        homePageFragment = new HomePageFragment();
        buyACarFragment= new BuyACarFragment();
        housePurchaseFragment=new HousePurchaseFragment();
        calledSingleFragment=new CalledSingleFragment();
        mineFragment=new MineFragment();

        fragmentList = new ArrayList<>();
        fragmentList.add(homePageFragment);
        fragmentList.add(buyACarFragment);
        fragmentList.add(housePurchaseFragment);
        fragmentList.add(calledSingleFragment);
        fragmentList.add(mineFragment);

    }
    /**
     * 跳转Fragment页面
     */
    private void loadFragment(int index) {
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        for (int i = 0; i < fragmentList.size(); i++) {
            Fragment fragment = fragmentList.get(i);
            if (i == index) {
                if (!fragment.isAdded()) {
                    ft.add(R.id.home_page_enter, fragment);
                }
                ft.show(fragment);
            } else {
                if (fragment.isAdded()) {
                    ft.hide(fragment);
                }
            }
        }
        ft.commitAllowingStateLoss();
    }
    //退出时调用
  public void  processout(){
      mineFragment.processemptying();
      lastPosition = 0;
      loadFragment(0);
      ((RadioButton) narMenu.getChildAt(lastPosition)).setChecked(true);
  }
private void Location(){
    // 定位初始化
    mLocClient = new LocationClient(this);
    MyLocationListenner myListener=new MyLocationListenner();
    mLocClient.registerLocationListener(myListener);
    LocationClientOption option = new LocationClientOption();
    option.setOpenGps(true); // 打开gps
    option.setCoorType("bd09ll"); // 设置坐标类型
    option.setScanSpan(0);
    mLocClient.setLocOption(option);
    mLocClient.start();
}
    /**
     * 定位SDK监听函数
     */
    public class MyLocationListenner implements BDLocationListener {

        @Override
        public void onReceiveLocation(BDLocation location) {
        String city =location.getCity();    //获取城市信息
            if (city!=null){
                if (isFirstLoc) {
                    isFirstLoc = false;
                    System.out.println("stringmd5"+city);
                    homePageFragment.processscrollTo(city);
                }
            }
        }
        public void onReceivePoi(BDLocation poiLocation) {
        }
    }

}
